package pl.veneo.wigprofit.view.elements {
	import pl.veneo.wigprofit.model.Configuration;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import pl.veneo.wigprofit.model.items.PercentData;
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import pl.veneo.wigprofit.tools.TimeSpan;
	import pl.veneo.wigprofit.model.PeriodData;
	import pl.veneo.wigprofit.model.items.IndexData;

	import flash.display.Sprite;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class ChartLines extends Sprite {
		
		private static const MAGINIFIER: Number = 1;
		
		private var _width : Number;
		private var _height : Number;
		private var _indexLine: Sprite;
		private var _percentLine: Sprite;
		private var _indexSnap: Bitmap;
		private var _perSnap: Bitmap;
		private var _mask : Sprite;

		public function ChartLines(width : Number, height : Number) {
			
			_height = height * MAGINIFIER;
			_width = width * MAGINIFIER;
			
			//_indexLine = new Sprite();
			//_percentLine = new Sprite();
			
			_indexLine = addChild(new Sprite()) as Sprite;
			_percentLine = addChild(new Sprite()) as Sprite;
			_mask = addChild(new Sprite()) as Sprite;
			_mask.graphics.beginFill(0xFF0000,1);
			_mask.graphics.drawRect(0, 0, width, height);
			mask = _mask;
			
		}
		
		public function showChart1(value: Boolean) : void {
			
			_indexLine.visible = value;
			
		}
		
		public function showChart2(value: Boolean) : void {
			
			_percentLine.visible = value;
			
		}
		
		public function resizeView(offsetX: Number, scaleFactor: Number ) : void {
					
			_indexLine.scaleX = 1/scaleFactor/ MAGINIFIER;
			_percentLine.scaleX = 1/scaleFactor/ MAGINIFIER;
			_indexSnap.scaleX = 1/scaleFactor/ MAGINIFIER;
			_perSnap.scaleX = 1/scaleFactor/ MAGINIFIER;
			_indexLine.x = 0 - ((_indexLine.width*MAGINIFIER) - _width)*offsetX;
			_percentLine.x = 0 - ((_percentLine.width*MAGINIFIER) - _width)*offsetX;
			_indexSnap.x = 0 - offsetX;
			_perSnap.x = 0 - offsetX;
			
		}

		public function draw(period: PeriodData) : void {
			
			//cacheAsBitmap = true;
			_indexLine.graphics.clear();
			_indexLine.graphics.lineStyle(0,Configuration.getInstance().indexColor,1,true,LineScaleMode.NONE,CapsStyle.NONE,JointStyle.MITER,1.414);
			_percentLine.graphics.clear();
			_percentLine.graphics.lineStyle(0,Configuration.getInstance().percentColor,0.8,true,LineScaleMode.NONE,CapsStyle.NONE,JointStyle.MITER,1.414);
			
			var indexItem: IndexData;	
			var percentItem: PercentData;
			var currentDay: int;
			var currentX: Number;	
			var min: Number = period.minIndex;
			var max: Number = period.maxIndex;
			
			var overscale: Number = (max - min) * 0.05;
			min -= overscale;
			max += overscale; 
	
			for(var i:uint=0;i<period.indexes.length;i++){
				
				indexItem = period.indexes[i] as IndexData;
				percentItem = period.percents[i] as PercentData;
				currentDay = TimeSpan.fromDates(period.startDay, indexItem.date).days;
				currentX = fitInXGrid(period.days, currentDay);
				
				if(i==0){
				_indexLine.graphics.moveTo(currentX, fitInYGrid(min, max, indexItem.index));
				_percentLine.graphics.moveTo(currentX, fitInYGrid(0, 100, percentItem.percent));
				}
				else{
				_indexLine.graphics.lineTo(currentX, fitInYGrid(min, max, indexItem.index));
				_percentLine.graphics.lineTo(currentX, fitInYGrid(0, 100, percentItem.percent));
				}
				
			}
			
			var _indexBitmap : BitmapData = new BitmapData(_width, _height,true, 0x00FFFFFF);
			_indexBitmap.draw(_indexLine);
			_indexSnap = new Bitmap(_indexBitmap,'auto',true);
			//addChild(_indexSnap);
			
			var _perBitmap : BitmapData = new BitmapData(_width, _height, true, 0x00FFFFFF);
			_perBitmap.draw(_percentLine);
			_perSnap = new Bitmap(_perBitmap,'auto',true);
			//addChild(_perSnap);
			
			_perSnap.scaleX = 1 / MAGINIFIER;
			_perSnap.scaleY = 1 / MAGINIFIER;
			
			_indexSnap.scaleX = 1 / MAGINIFIER;
			_indexSnap.scaleY = 1 / MAGINIFIER;
			
			_percentLine.scaleX = 1 / MAGINIFIER;
			_percentLine.scaleY = 1 / MAGINIFIER;
			
			_indexLine.scaleX = 1 / MAGINIFIER;
			_indexLine.scaleY = 1 / MAGINIFIER;
			
			trace((_indexLine.width * MAGINIFIER)+" >>> "+_width);
			
		}

		private function fitInXGrid(days : int, currentDay : int) : Number {
			
			var step: Number = _width / days;
			return currentDay * step;
			
		}

		private function fitInYGrid(min : Number, max : Number, value : Number) : Number {
			
			var step : Number = _height / ( max - min ); 
			return _height - ((value - min) * step);
			
		}
		
		
	}
}
